import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import types from "../view/type";
import user from "../view/user";
import department from "../view/department";
import statistical from "../view/statistical";
import Asido from "../view/Asido";
import login from "../view/login";
let routes = new VueRouter({
  routes: [
    {
      path: "/",
      // 重定向，当页面为 / 时就跳转到Home页面
      redirect: "login",
    },
    {
      path: "/login",
      name: "login",
      component: login,
    },
    {
      path: "/Asido",
      component: Asido,
      children: [
        {
          path: "/",
          component: user,
        },
        {
          path: "types",
          component: types,
        },
        {
          path: "user",
          name:'user',
          component: user,
        },
        {
          path: "department",
          component: department,
        },
        {
          path: "statistical",
          component: statistical,
        },
      ],
    },
  ],
});

routes.beforeEach((to, from, next) => {
  let token = window.localStorage.getItem("token");
  if (!token && to.name !== "login") {
    next({ name: "login" });
  } else if (token && to.name === "login") {
    next({
      name: "user",
    });
  } else {
    next();
  }
});

export default routes;
